define(function (require) {
    var ko = require("knockout");
    var komapping = require("komapping");
    var router = require("plugins/router");
    var $ = require("jquery");
    require("jqueryCookie");
    var shareUrlContent = require("text!./share-url-content.html");
    var pasteUrlParametersContent = require("text!./paste-url-parameters-content.html");
    var ParamCommon = require("./ParamCommon");

    var param = {
        searchDate: ko.observable(""),
        actionId: ko.observable(""),
        codeArrayInclude: ko.observable(""),
        codeArrayExclude: ko.observable(""),
        hostInclude: ko.observable(""),
        hostExclude: ko.observable(""),
        refererInclude: ko.observable(""),
        refererExclude: ko.observable(""),
        protocolInclude: ko.observable(""),
        protocolExclude: ko.observable(""),
        urlInclude: ko.observable(""),
        urlExclude: ko.observable(""),
        typeInclude: ko.observable(""),
        typeExclude: ko.observable(""),
        minSize: ko.observable("-1")
    };

    //当前的用户 id
    var modelId = null;

    return {
        activate: function (id, queryParam) {
            modelId = id;
            ParamCommon.setParamFromUrl(param, queryParam);
        },
        param: param,
        compositionComplete: function () {
            layui.use(["laytpl", "table", "element", "layer", "form", "laydate"], function () {
                var laytpl = layui.laytpl;
                var form = layui.form;
                var layer = layui.layer;
                var element = layui.element;
                var laydate = layui.laydate;

                if (!param.actionId()) {
                    laydate.render({
                        elem: "#search-time",
                        type: "datetime",
                        format: "yyyy-MM-dd HH:mm:ss",
                        value: param.searchDate(),
                        done: function (value, date, endDate) {
                            param.searchDate(value);
                        }
                    });
                }

                $("#share-url").click(function () {
                    var url = ParamCommon.searchUrlGen(param, "http://" + window.location.host + "/kunlunAssets/index.html#plan/review/param/" + modelId);
                    layer.open({
                        content: shareUrlContent,
                        shadeClose: true,
                        btn: [],
                        title: "请复制",
                        success: function (layero, index) {
                            var inputDom = $(layero.selector).find(".share-url-input");
                            inputDom.val(url);
                            inputDom.select();
                        }
                    });
                    return false;
                });

                $("#share-params-only").click(function () {
                    var paramObj = komapping.toJS(param);
                    delete paramObj.searchDate;
                    delete paramObj.actionId;
                    layer.open({
                        content: shareUrlContent,
                        shadeClose: true,
                        btn: [],
                        title: "请复制",
                        success: function (layero, index) {
                            var inputDom = $(layero.selector).find(".share-url-input");
                            inputDom.val(JSON.stringify(paramObj));
                            inputDom.select();
                        }
                    });
                    return false;
                });

                $("#paste-params").click(function () {
                    layer.open({
                        content: pasteUrlParametersContent,
                        shadeClose: true,
                        area: "314px",
                        btn: [],
                        title: "请粘贴",
                        success: function (layero, index) {
                            form.render();
                            var inputDom = $(layero.selector).find(".paste-parameter-input");
                            inputDom.select();
                            form.on("submit(paste-parameter-submit)", function (data) {
                                try {
                                    var obj = JSON.parse(data.field.pasteParameter);
                                    komapping.fromJS(obj, {}, param);
                                } catch (e) {
                                    console.error(e);
                                    layer.alert("Json 格式错误");
                                }
                                layer.close(index);
                                return false;
                            });
                        }
                    });
                    return false;
                });

                form.on("submit(search-submit)", function (data) {
                    router.navigate(ParamCommon.searchUrlGen(param, "plan/review/" + modelId));
                    return false;
                });

                form.on("submit(images-submit)", function (data) {
                    router.navigate(ParamCommon.searchUrlGen(param, "plan/images/" + modelId));
                    return false;
                });
            });
        }
    };
});